<template>
  <div class="user">
    <el-row>
      <el-col :span="24">
        <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <!-- 给组件绑定原生事件的话，需要一个.native的修饰符 -->
        <el-input placeholder="请输入内容" v-model="keys.key" class="search-input" @keydown.enter.native="getDataList(keys)">
          <el-button slot="append" icon="el-icon-search" @click="getDataList(keys)"></el-button>
        </el-input>
        <el-button type="success" plain @click="addDialogFormVisible=true">添加用户</el-button>
      </el-col>
    </el-row>

    <el-table
      class="margin-20"
      :data="List"
      v-loading="loading"
      border
      style="width: 100%">
      <el-table-column
      prop="tokenId"
      label="编号"
      width="50">
      </el-table-column>
      <el-table-column
        prop="companyLogo"
        label="logo"
        width="130">
       <template scope="scope">
          <img width="80" :src="scope.row.companyLogo" alt="">
       </template>
      </el-table-column>
      <el-table-column
        prop="companyName"
        label="公司名称"
        width="130">
      </el-table-column>
      <el-table-column
        prop="positionName"
        label="职位">
      </el-table-column>
      <el-table-column
        prop="salary"
        label="薪资">
      </el-table-column>
      <el-table-column
        prop="degree"
        label="学历要求">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini"
            type="primary"
            plain 
            icon="el-icon-edit"
            @click="showEditDialog(scope.row)">
          </el-button>
          <el-button size="mini"
            type="danger"
            plain 
            icon="el-icon-delete"
            @click="showDeleteDialog(scope.row)">
          </el-button>
          <el-button size="mini"
            type="warning" 
            plain
            icon="el-icon-check"
            @click="showGrantDialog(scope.row)">
          </el-button>
        </template>
      </el-table-column>
      
    </el-table>

    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[4,6,8,10]"
        :page-size="1"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!-- 添加用户对话框 -->
    <el-dialog title="添加用户" :visible.sync="addDialogFormVisible">
      <el-form :model="addForm" label-width="80px" :rules="rules" ref="addUserForm">
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="addForm.companyName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="职位名称" prop="positionName">
          <el-input v-model="addForm.positionName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="工作城市" prop="city">
          <el-input v-model="addForm.city" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="薪资要求" prop="salary">
          <el-input v-model="addForm.salary" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="学历" prop="degree">
          <el-input v-model="addForm.degree" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="工作经验" prop="experience">
          <el-input v-model="addForm.experience" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="工作类型" prop="type">
          <el-input v-model="addForm.type" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="description" prop="description">
          <el-input v-model="addForm.description" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="公司logo">
          <input type='file' ref="upload" auto-complete="off">
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUserSubmit('addUserForm')">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑用户对话框 -->
    <el-dialog title="编辑用户" :visible.sync="editDialogFormVisible">
      <el-form :model="editForm" label-width="80px" ref="addUserForm">
        <el-form-item label="公司logo" prop="companyLogo">
           <img :src="editForm.companyLogo" alt="" class="logo">
        </el-form-item>
        <el-form-item label="公司logo" prop="companyLogo">
          <input type='file' ref="editUpload" auto-complete="off">
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="editForm.companyName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="职位名称" prop="positionName">
          <el-input v-model="editForm.positionName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="工作城市" prop="city">
          <el-input v-model="editForm.city" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="工作类型" prop="type">
          <el-input v-model="editForm.type" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="薪资要求" prop="salary">
          <el-input v-model="editForm.salary" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="学历" prop="degree">
          <el-input v-model="editForm.degree" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="工作经验" prop="experience">
          <el-input v-model="editForm.experience" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="工作描述" prop="de">
          <el-input v-model="editForm.description" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserSubmit('editUserForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getList } from "@/api";
import { addList } from "@/api";
import { update } from "@/api";
export default {
  data() {
    return {
      total: 10,
      count: 5,
      loading: 10,
      List: [],
      keys: {
        key: ""
      },
      query: {
        //这个是页码和数量初始化的值
        page: 1,
        count: 4
      },
      addDialogFormVisible: false,
      editDialogFormVisible: false,
      addForm: {
        companyName: "",
        positionName: "",
        city: "",
        salary: 0,
        degree: "",
        type: "",
        experience: "",
        description: ""
      },
      editForm: {
        companyName: "",
        positionName: "",
        city: "",
        salary: 0,
        degree: "",
        type: "",
        experience: "",
        description: "",
        companyLogo: "",
        tokenId: ""
      },
      key: "",
      rules: {
        companyName: [
          { required: true, message: "请输入公司名称", trigger: "blur" }
        ],
        positionName: [
          { required: true, message: "请输入职位名称", trigger: "blur" }
        ],
        city: [
          { required: true, message: "请输入工作地址地址", trigger: "blur" }
        ],
        salary: [
          { required: true, message: "请输入薪资要求", trigger: "blur" }
        ],
        degree: [
          { required: true, message: "请输入学历要求", trigger: "blur" }
        ],
        type: [{ required: true, message: "请输入工作类型", trigger: "blur" }],
        experience: [
          { required: true, message: "请输入工作经验", trigger: "blur" }
        ],
        description: [
          { required: true, message: "请输入工作描述", trigger: "blur" }
        ]
      }
    };
  },
  mounted() {
    this.getDataList(this.query);
  },
  methods: {
    handleSizeChange(val) {
      //切换显示条数事件
      this.query.count = val;
      //重新请求数据
      this.getDataList(this.query);
    },
    handleCurrentChange(val) {
      //切换页码事件
      this.query.page = val;
      this.getDataList(this.query);
    },
    getDataList(query) {
      //获取数据
      return getList(query).then(res => {
        console.log(res);
        if (res.status === 200) {
          this.List = res.data.result;
          //改变分页器上面的总数
          this.total = res.data.count;
          this.loading = false;
          return res;
          console.log(this.list);
        }
      });
    },
    addUserSubmit(formName) {
      this.$refs[formName].validate(valide => {
        if (valide) {
          // 在这里收集所有的表单信息 包括图片 传到api 再由api传送到后台
          //把获取到的图片添加给addForm
          this.addForm.companyLogo = this.$refs.upload.files[0];
          addList(this.addForm).then(res => {
            if (res.data.msg) {
              //上传完成之后需要重新刷新列表所有要重新发请求
              this.getDataList(this.query);
            }
          });
          this.addDialogFormVisible = false;
        }
      });
    },
    showEditDialog(row) {
      this.editDialogFormVisible = true;
      getList({ tokenId: row.tokenId }).then(res => {
        if (res.status == 200) {
          this.editForm = res.data.result[0];
          //更新完成之后重新获取列表
        }
      });
    },
    editUserSubmit(formName) {
      //更新操作
      const logo = this.$refs.editUpload.files[0];
      this.editDialogFormVisible = false;
      if (logo) {
        this.editForm.companyLogo = logo;
      }
      update(this.editForm).then(res => {
        if (res.status == 200) {
          this.$message({
            type: "success",
            message: "更新成功"
          });
          this.getDataList();
          this.editDialogFormVisible = false;
        }
      });
    },
    showDeleteDialog(row) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          console.log(row.id);
          deleteUser(row.id).then(res => {
            if (res.meta.status === 200) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getDataList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.logo {
  width: 120px;
}
.user {
  .margin-20 {
    margin: 20px 0;
  }
  .search-input {
    width: 300px;
  }
  .page {
    padding: 5px 0;
    background-color: #d3dce6;
  }
}
</style>
